<?php

namespace app\service;

use app\model\ClueCommunicateRecord;
use app\model\ClueMobile;
use app\model\UserOrganization;
use think\facade\Db;

class ClueEnterprise
{
    const AREA_LEVEL = [
        1 => 'province',
        2 => 'city',
        3 => 'area',
    ];

    public function spliceWhere($params = [])
    {
        $clueEnterprise = new \app\model\ClueEnterprise();
        $enter_ids = $clueEnterprise->getIds();

        $communicationRecord = new ClueCommunicateRecord();
        //企业姓名
        if (isset($params['enterprise_name']) && $params['enterprise_name']) {
//            $list = $clueEnterprise
//                ->where('enterprise_name', 'like', '%' . $params['enterprise_name'] . '%')
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $clueEnterprise
                ->where('enterprise_name', 'like', '%' . $params['enterprise_name'] . '%')
                ->column ('id');
            //两个数组的交集并赋值给person_ids
            $enter_ids = $this->handlePersonIds($enter_ids, $ids);
        }

        //联系人
        if (isset($params['contacts']) && $params['contacts']) {

            //每个人才最新的联系人
            $last_mobile =Db::table('clue_mobile')
                ->where('type',2)
                ->field ('Max(create_date) as create_date,clue_p_id,name')
                ->whereNotNull('clue_p_id')
                ->group('clue_p_id')
                ->select()
                ->toArray();

            $ids = [];
            foreach($last_mobile as $item){
                $info = Db::table('clue_mobile')
                    ->where('create_date',$item['create_date'])
                    ->where('clue_p_id',$item['clue_p_id'])
                    ->whereLike('name','%'.$params['contacts'].'%')
                    ->value('id');
                if(!$info){
                    continue;
                }
                $ids[] = $item['clue_p_id'];
            }

            //两个数组的交集并赋值给person_ids
            $enter_ids = $this->handlePersonIds($enter_ids, $ids);
        }
        //企业类型
        if (isset($params['enterprise_type']) && $params['enterprise_type']) {
//            $list = $clueEnterprise
//                ->where('enterprise_type', $params['enterprise_type'] )
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');

            $ids = $clueEnterprise
                ->where('enterprise_type', $params['enterprise_type'] )
                ->column ('id');

            //两个数组的交集并赋值给person_ids
            $enter_ids = $this->handlePersonIds($enter_ids, $ids);
        }
        //电话
        if (isset($params['mobile']) && $params['mobile']) {

            //每个人才最新的电话
            $last_mobile =Db::table('clue_mobile')
                ->where('type',2)
                ->field ('Max(create_date) as create_date,clue_p_id,mobile')
                ->whereNotNull('clue_p_id')
                ->group('clue_p_id')
                ->select()
                ->toArray();

            $ids = [];
            foreach($last_mobile as $item){
                $info = Db::table('clue_mobile')
                    ->where('create_date',$item['create_date'])
                    ->where('clue_p_id',$item['clue_p_id'])
                    ->whereLike('mobile','%'.$params['mobile'].'%')
                    ->value('id');
                if(!$info){
                    continue;
                }
                $ids[] = $item['clue_p_id'];
            }

            //两个数组的交集并赋值给person_ids
            $enter_ids = $this->handlePersonIds($enter_ids, $ids);
        }
        //区域
        if (isset($params['area']) && $params['area']) {
//            $list = $clueEnterprise->where(self::AREA_LEVEL[$params['area_level']], $params['area'])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');

            $ids = $clueEnterprise->where(self::AREA_LEVEL[$params['area_level']], $params['area'])->column('id');
            //两个数组的交集并赋值给person_ids
            $enter_ids = $this->handlePersonIds($enter_ids, $ids);
        }

        //创建时间
        if ((isset($params['create_start_date']) && $params['create_start_date']) && (isset($params['create_end_date']) && $params['create_end_date'])) {

            $start_time = $params['create_start_date'] . ' 00:00:00';
            $end_time = $params['create_end_date'] . ' 23:59:59';

//            $list = $clueEnterprise->where('create_date', 'between', [$start_time, $end_time])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $clueEnterprise->where('create_date', 'between', [$start_time, $end_time])
                ->column('id');
            //两个数组的交集并赋值给person_ids
            $enter_ids = $this->handlePersonIds($enter_ids, $ids);
        }
        //成立时间
        if ((isset($params['establish_start_date']) && $params['establish_start_date']) && (isset($params['establish_end_date']) && $params['establish_end_date'])) {
            $start_time = $params['establish_start_date'] . ' 00:00:00';
            $end_time = $params['establish_end_date'] . ' 23:59:59';

//            $list = $clueEnterprise->where('establish_date', 'between', [$start_time, $end_time])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $clueEnterprise->where('establish_date', 'between', [$start_time, $end_time])
                ->column ('id');
            //两个数组的交集并赋值给person_ids
            $enter_ids = $this->handlePersonIds($enter_ids, $ids);
        }
        //安全到期时间
        if ((isset($params['secure_expire_start_date']) && $params['secure_expire_start_date']) && (isset($params['secure_expire_end_date']) && $params['secure_expire_end_date'])) {
            $start_time = $params['secure_expire_start_date'] . ' 00:00:00';
            $end_time = $params['secure_expire_end_date'] . ' 23:59:59';
//            $list = $clueEnterprise->where('secure_expire_date', 'between', [$start_time, $end_time])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $clueEnterprise->where('secure_expire_date', 'between', [$start_time, $end_time])
                ->column('id');
            //两个数组的交集并赋值给person_ids
            $enter_ids = $this->handlePersonIds($enter_ids, $ids);
        }

        //资质类别
        if (isset($params['qualifications_type']) && $params['qualifications_type']) {
//            $list = $clueEnterprise
//                ->where('qualifications_type', $params['qualifications_type'])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $clueEnterprise
                ->where('qualifications_type', $params['qualifications_type'])
                ->column ('id');
            //两个数组的交集并赋值给person_ids
            $enter_ids = $this->handlePersonIds($enter_ids, $ids);
        }
        //分配时间
        if (isset($params['allocation_start_date'], $params['allocation_end_date']) && $params['allocation_start_date'] && $params['allocation_end_date']) {
            $params['allocation_start_date'] .= ' 00:00:00';
            $params['allocation_end_date'] .= ' 23:59:59';
//            $list = $clueEnterprise->where('allocation_date', 'between', [$params['allocation_start_date'], $params['allocation_end_date']])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $clueEnterprise->where('allocation_date', 'between', [$params['allocation_start_date'], $params['allocation_end_date']])
                ->column ('id');
            $enter_ids = $this->handlePersonIds($enter_ids, $ids);
        }
        //沟通时间
//        if ((isset($params['new_communicate_start_date']) && $params['new_communicate_start_date']) && (isset($params['new_communicate_end_date']) && $params['new_communicate_end_date'])) {
//            $params['new_communicate_start_date'] = $params['new_communicate_start_date'] . ' 00:00:00';
//            $params['new_communicate_end_date'] = $params['new_communicate_end_date'] . ' 23:59:59';
//            $communicationRecord = new ClueCommunicateRecord();
//            $list = $communicationRecord->where('communicate_date', 'between', [$params['allocation_start_date'], $params['allocation_end_date']])
//                ->where('type', 2)
//                ->field('clue_p_id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'clue_p_id');
//            $enter_ids = $this->handlePersonIds($enter_ids, $ids);
//        }
        //下次沟通时间
        if (isset($params['next_start_communicate_date'], $params['next_end_communicate_date']) && $params['next_start_communicate_date'] && $params['next_end_communicate_date']) {
            $params['next_start_communicate_date'] .= ' 00:00:00';
            $params['next_end_communicate_date'] .= ' 23:59:59';
//            $list = $communicationRecord->where('next_communicate_date', 'between', [$params['next_start_communicate_date'], $params['next_end_communicate_date']])
//                ->where('type', 2)
//                ->field('clue_p_id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'clue_p_id');
            $ids = $communicationRecord->where('next_communicate_date', 'between', [$params['next_start_communicate_date'], $params['next_end_communicate_date']])
                ->where('type', 2)
                ->column('clue_p_id');


            $enter_ids = $this->handlePersonIds($enter_ids, $ids);
        }
        //最新沟通时间
        if ((isset($params['new_communicate_start_date']) && $params['new_communicate_start_date']) && (isset($params['new_communicate_end_date']) && $params['new_communicate_end_date'])) {
            $params['new_communicate_start_date'] .= ' 00:00:00';
            $params['new_communicate_end_date'] .= ' 23:59:59';

//            $list = $communicationRecord->where('communicate_date', 'between', [$params['new_communicate_start_date'], $params['new_communicate_end_date']])
//                ->where('type', 2)
//                ->field('clue_p_id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'clue_p_id');
            $ids = $communicationRecord->where('communicate_date', 'between', [$params['new_communicate_start_date'], $params['new_communicate_end_date']])
                ->where('type', 2)
                ->column('clue_p_id');

            $enter_ids = $this->handlePersonIds($enter_ids, $ids);
        }
        //沟通记录
        if (isset($params['communicate_content']) && $params['communicate_content']) {
//            $list = $communicationRecord
//                ->where('communicate_content', 'like', '%' . $params['communicate_content'] . '%')
//                ->where('type', 2)
//                ->field('clue_p_id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'clue_p_id');

            $ids = $communicationRecord
                ->where('communicate_content', 'like', '%' . $params['communicate_content'] . '%')
                ->where('type', 2)
                ->column('clue_p_id');

            //两个数组的交集并赋值给person_ids
            $enter_ids = $this->handlePersonIds($enter_ids, $ids);
        }
        //意向结果
        if (isset($params['intention']) && $params['intention']) {
//            $list = $communicationRecord
//                ->where('intention', $params['intention'])
//                ->where('type', 2)
//                ->field('clue_p_id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'clue_p_id');

            $ids = $communicationRecord
                ->where('intention', $params['intention'])
                ->where('type', 2)
                ->column('clue_p_id');

            //两个数组的交集并赋值给person_ids
            $enter_ids = $this->handlePersonIds($enter_ids, $ids);
        }
        //沟通结果
        if (isset($params['call']) && $params['call']) {
            $call_clue_ids = $communicationRecord
//                ->where('call', $params['call'])
                ->where('type', 2)
                ->field('id,clue_p_id,call')
                ->order('communicate_date desc')
//                ->group('clue_p_id')
                ->select()
                ->toArray();
            $ids = [];
            foreach($call_clue_ids as $item){
                if(!in_array($item['clue_p_id'],$ids) && $item['call']['id'] == $params['call']){
                    $ids[] = $item['clue_p_id'];
                }
            }
//            $ids = array_column($list, 'clue_p_id');
            //两个数组的交集并赋值给person_ids
            $enter_ids = $this->handlePersonIds($enter_ids, $ids);
        }
        //负责人
        if (isset($params['f_uid']) && $params['f_uid']) {
            $userModel = new \app\model\User();
            //模糊查询用户id
            $userId = $userModel
                ->whereLike ('nickname','%'.$params['f_uid'].'%')
                ->column ('id');

//            $list = $clueEnterprise
//                ->where('f_uid', 'in', $userId)
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $clueEnterprise
                ->where('f_uid', 'in', $userId)
                ->column('id');

            //两个数组的交集并赋值给person_ids
            $enter_ids = $this->handlePersonIds($enter_ids, $ids);
        }
        //订单状态
//        if (isset($params['status']) && $params['status']) {
//            $list = $clueEnterprise
//                ->where('status', $params['status'])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
//            //两个数组的交集并赋值给person_ids
//            $enter_ids = $this->handlePersonIds($enter_ids, $ids);
//        }
        //是否分配
        if (isset($params['is_f']) && $params['is_f']) {
//            $list = $clueEnterprise
//                ->where('is_f', $params['is_f'])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $clueEnterprise
                ->where('is_f', $params['is_f'])
                ->column('id');

            //两个数组的交集并赋值给person_ids
            $enter_ids = $this->handlePersonIds($enter_ids, $ids);
        }

        //备注
        if (isset($params['notes']) && $params['notes']) {
//            $list = $clueEnterprise
//                ->whereLike('notes', '%'.$params['notes'].'%')
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');

            $ids = $clueEnterprise
                ->whereLike('notes', '%'.$params['notes'].'%')
                ->column('id');

            //两个数组的交集并赋值给person_ids
            $enter_ids = $this->handlePersonIds($enter_ids, $ids);
        }

        //是否分配
        if (isset($params['is_f']) && $params['is_f']) {
//            $list = $clueEnterprise
//                ->where('is_f', $params['is_f'])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $clueEnterprise
                ->where('is_f', $params['is_f'])
                ->column('id');

            //两个数组的交集并赋值给person_ids
            $enter_ids = $this->handlePersonIds($enter_ids, $ids);
        }

        //人员查询
        if (isset($params['uid']) && $params['uid']){

            $ids = $clueEnterprise
                ->where('f_uid', $params['uid'])
                ->column ('id');

            //两个数组的交集并赋值给person_ids
            $enter_ids = $this->handlePersonIds($enter_ids, $ids);
        }

        //部门查询
        if (isset($params['org_id']) && $params['org_id']){
            //查询该组织下所有部门&& 所有人员
            $systemService = new System();
            $orgModel = new UserOrganization();
            $userModel = new \app\model\User();
            $orgStr = $systemService->getOrgIdStr($params['org_id'],$orgModel->getStructure ());
            $uidArr = $userModel->getDepartmentIdsUserIds ($orgStr);

            $ids = $clueEnterprise
                ->whereIn('f_uid', $uidArr)
                ->column ('id');

            //两个数组的交集并赋值给person_ids
            $enter_ids = $this->handlePersonIds($enter_ids, $ids);
        }

        return $enter_ids;
    }

    public function handlePersonIds($enter_ids, $other_ids)
    {
        if (!$enter_ids || !$other_ids) {
            return [];
        }
        if ($enter_ids && $other_ids) {
            $enter_ids = array_intersect($enter_ids, $other_ids);
        } elseif ($enter_ids && !$other_ids) {
            $enter_ids = [];
        } elseif (!$enter_ids && $other_ids) {
            $enter_ids = array_intersect($enter_ids, $other_ids);
        } elseif (!$enter_ids && !$other_ids) {
            $enter_ids = [];
        }
        return $enter_ids;
    }
}